<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="visible"
    width="720px"
    @close="reset"
  >

    <el-form ref="form" :rules="rules" class="form" :model="query" label-width="90px">
      <el-form-item label="单据编号:" class="merge-item">
        <el-input v-model.trim="query.billCode" :disabled="true" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="缴存门店:" prop="orgSNNo">
        <el-tree-select
          v-model="query.orgSNNo"
          :disabled="false"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择组织机构',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @node-click="selectTreeChange"
        />
      </el-form-item>

      <el-form-item label="缴存人:" class="merge-item">
        <el-input v-model.trim="query.operator" :disabled="true" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="缴存时间:" class="merge-item" prop="startDt">
        <el-date-picker
          v-model="query.handInTime"
          :disabled="true"
          style="width: 100%;"
          clearable
          type="datetime"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="缴存类型:" class="merge-item block" prop="handInType">
        <el-select v-model="query.handInType" style="width: 100%;">
          <el-option v-for="(l,i) in handInTypeList" :key="i" :label="l.desc" :value="l.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="缴存项:" class="merge-item block" prop="handInItem">
        <el-select v-model="query.handInItem" style="width: 100%;">
          <el-option v-for="(l,i) in handInItemList" :key="i" :label="l.text" :value="l.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="金额:" class="merge-item block" prop="amount">
        <el-input v-model="query.amount" placeholder="请输入" clearable>
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="账户:" class="merge-item block" prop="cardNo">
        <el-select v-model="query.cardNo" style="width: 100%;">
          <el-option v-for="(l,i) in bankList" :key="i" :label="`${l.cardNo} ${l.accountName}`" :value="l.cardNo" />
        </el-select>
      </el-form-item>
      <el-form-item prop="remark" class="block">
        <label slot="label">备注:</label>
        <el-input v-model="query.remark" type="textarea" clearable :rows="3" maxlength="255" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.success " type="primary" @click="success">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { handindayrecordSave, handindayrecordSearchEnum } from '@/api'
  import { mapGetters } from 'vuex'
  export default {
    name: 'add',
    components: {

    },
    props: {
      treeData: {
        type: Array,
        default: []
      },
      billStatusList: {
        type: Array,
        default: []
      },
      handInTypeList: {
        type: Array,
        default: []
      },
      orgList: {
        type: Array,
        default: []
      }
    },
    data() {
      return {
        visible: false,
        title: null,
        type: 'add',
        handInItemList: [],
        bankList: [],
        rules: {
          orgSNNo: [{ required: true, message: '请选择', trigger: 'blur' }],
          handInType: [{ required: true, message: '请选择', trigger: 'blur' }],
          handInItem: [{ required: true, message: '请输入', trigger: 'blur' }],
          amount: [{ required: true, message: '请输入', trigger: 'blur', pattern: /(?:^[1-9]([0-9]+)?(?:\.[0-9]{1,2})?$)|(?:^(?:0)$)|(?:^[0-9]\.[0-9](?:[0-9])?$)/ }],
          cardNo: [{ required: true, message: '请选择', trigger: 'blur' }]
          // cashAmount: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        orgObj: {},
        query: {
          billCode: null,
          orgSNNo: null,
          operator: null,
          handInTime: null,
          handInType: null,
          handInItem: null,
          amount: null,
          cardNo: null,
          remark: null
        }
      }
    },
    computed: {
      ...mapGetters([
        'userInfo'
      ])
    },
    mounted() {
      this.$watch('query.handInType', (n) => {
        var datas = []
        if (n === 1) {
          datas.push({ text: '其他收入', value: 17 })
          datas.push({ text: '配件收入', value: 20 })
          datas.push({ text: '以前月结收回', value: 21 })
          datas.push({ text: '调整收入', value: 22 })
          datas.push({ text: '过户', value: 25 })
          datas.push({ text: '补卡', value: 26 })
        } else if (n === 2) {
          datas.push({ text: '交款', value: 204 })
          datas.push({ text: '费用支出', value: 205 })
          datas.push({ text: '现金券', value: 206 })
          datas.push({ text: '调整支出', value: 208 })
        } else {
          datas = []
        }
        this.handInItemList = datas
      })
    },
    methods: {
      open(param) {
        this.visible = true
        this.type = 'add'
        this.title = '新增缴存单'
        this.query.handInTime = new Date() // 默认时间
        this.query.operator = this.userInfo.userName // 默认缴存人
        if (param) {
          this.type = 'edit'
          this.title = `编辑缴存单`
          // console.log({ ...param })
          this.query = { ...this.query, ...param }
          this.selectTreeChange()
          this.query = { ...this.query, ...param }
        }
      },
      async success() {
        try {
          var flag = await this.$refs.form.validate()
          if (flag) {
            const params = {
              ...this.query
            }
            var orgObj = this.orgList.find(l => this.query.orgSNNo === l.orgSNNo)
            params['orgName'] = orgObj.orgName
            params['orgId'] = orgObj.orgId
            params['accountName'] = this.bankList.find(l => l.cardNo === this.query.cardNo).accountName
            const res = await handindayrecordSave(params)
            if (res.success) {
              this.reset()
              this.$emit('refresh')
            }
          }
        } catch (e) {
          console.log(e)
        }
      },
      async  selectTreeChange(val) {
        // this.orgObj = val
        this.query.cardNo = ''
        const res = await handindayrecordSearchEnum()
        this.bankList = res.data
      },
      reset() {
        this.query = {
          billCode: null,
          orgSNNo: null,
          operator: null,
          handInTime: null,
          handInType: null,
          handInItem: null,
          amount: null,
          cardNo: null,
          remark: null
        }
        this.visible = false
      }
    }

  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  justify-content: space-between;
  width: 100%;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>

